﻿//
// XML Serializing
// Created by Martin Blore 2007
// 
// A small sample of the XML serialization functionality in .NET.
//
// Public Domain

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;

namespace XmlSettings
{
    public struct HttpSettings
    {
        [XmlElement()]
        public int ListenPort;
        [XmlElement()]
        public string Protocol;
        [XmlElement()]
        public string Username;
        [XmlElement()]
        public string Password;
    }
    public struct IrcSettings
    {
        [XmlElement()]
        public bool ircBotEnabled;
        [XmlElement(IsNullable = false)]
        public string ircBotPassword;
        [XmlElement()]
        public string ircServer;
        [XmlElement()]
        public int ircServerPort;
        [XmlElement()]
        public string ircNickPrefix;
        [XmlElement()]
        public string ircChannel;
        [XmlElement(IsNullable = false)]
        public string ircKey;
    }
    public struct TorrentSettings
    {
        //global torrent settings
        [XmlElement()]
        public string downloadsPath;
        [XmlElement()]
        public string torrentsPath;
        [XmlElement()]
        public int MinFreeSpace;
        [XmlElement()]
        public int MaxDownloadSpeed;
        [XmlElement()]
        public int MaxUploadSpeed;
        [XmlElement()]
        public int MaxConnections;
        [XmlElement()]
        public int MaxDownloads;
        [XmlElement()]
        public int MaxHalfOpenConnections;
        [XmlElement()]
        public bool AllowLegacyConnections;
        [XmlElement()]
        public int listenPort;
        [XmlElement()]
        public int MaxSimultaneousTorrents;//?
        [XmlElement()]
        public bool ResumeOnAppStart;
        [XmlElement()]
        public string listenAddress; //?
        //torrent default settings;
        [XmlElement()]
        public int defaultDownloadRate;
        [XmlElement()]
        public int defaultUploadRate;
        [XmlElement()]
        public int defaultMaxConn;
        [XmlElement()]
        public int defaultUploadSlots;


    }

    public struct RssFeed
    {
        [XmlAttribute()]
        public string Url;
        [XmlAttribute()]
        public string Filter;
        [XmlAttribute()]
        public bool Enabled;
    }
    [XmlRoot(Namespace = null, IsNullable = false, ElementName = "Settings")] //nullable fals?
    public class Settings
    {

        private string _ConfigPath;
        [XmlIgnore()]
        public string ConfigPath
        {
            get { return _ConfigPath; }
            set { _ConfigPath = value; }

        }

   
        public static Settings Load(string fileName)
        {
            FileStream stream = null;
            Settings organizer = null;
            if (!File.Exists(fileName)) //no config file present ? write default config to file
            {
                Console.WriteLine("ERROR LOADING SETTINGS. " + fileName + " NOT PRESENT");
                return null;
            }

            try
            {
                stream = File.OpenRead(fileName);
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                organizer = (Settings)serializer.Deserialize(stream);
            }

            catch (Exception e)
            {
                Console.WriteLine("ERROR LOADING SETTINGS. There was a problem deserialising the XML Data in " + fileName);
                Console.WriteLine("Please check the configuration, note that the XML tags are case sensitive");

            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            return organizer;
        }


        public List<RssFeed> RssFeeds = new List<RssFeed>();
        public TorrentSettings TorrentSettings;
        public IrcSettings IrcSettings;
        public HttpSettings HttpSettings;

        internal void Save(string p)
        {
            throw new NotImplementedException();
        }
    }


}
